package com.tj.jinzao.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.EditText;

import com.tj.jinzao.R;
import com.tj.jinzao.activity.CompleteInfoActivity;
import com.tj.jinzao.base.BaseFragment;
import com.tj.jinzao.callback.IRegisterLoginView;
import com.tj.jinzao.model.User;
import com.tj.jinzao.presenter.RegisterLoginPresenter;
import com.tj.jinzao.utils.ToastUtils;
import com.tj.jinzao.widgets.CustomDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 输入验证码
 * Created by wjy on 2019/1/5.
 */
public class InviteCodeFragment extends BaseFragment<RegisterLoginPresenter> implements IRegisterLoginView<User> {
    @BindView(R.id.et_code)
    EditText mCodeEditView;

    @Override
    protected RegisterLoginPresenter createPresenter() {
        return new RegisterLoginPresenter(this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_invite_code;
    }

    @OnClick(R.id.tv_skip)
    public void skip() {
        startActivity(new Intent(mContext, CompleteInfoActivity.class));
    }

    @OnClick(R.id.bt_confirm)
    public void getCode() {
        final String code = mCodeEditView.getText().toString().trim();
        if (!TextUtils.isEmpty(code)) {
//            mPresenter.getUserByRequestCode(code);
            mPresenter.bindRequestCode(code);
        } else {
            ToastUtils.showToast(mContext, "请输入邀请码");
        }
    }

    @Override
    public void onRequestSuccess(User user) {
        skip();
    }

    @Override
    public void onRequestNoData(String message) {

    }

    @Override
    public void onRequestError(String error_message) {
        CustomDialog dialog = new CustomDialog.Builder(mContext).setMessage(error_message)
                .setPositiveButton("确定").setNegativeButton("取消").setOnClickListener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(which == DialogInterface.BUTTON_POSITIVE){
                        }
                        dialog.dismiss();
                    }
                }).create();
        dialog.show();
    }

    @Override
    public void onGetCodeSuccess(String s) {
        if(s!=null&&!s.equals("")){
            CustomDialog dialog = new CustomDialog.Builder(mContext).setMessage("您的推荐人是"+s)
                    .setPositiveButton("确定").setNegativeButton("取消").setOnClickListener(new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if(which == DialogInterface.BUTTON_POSITIVE){
                                startActivity(new Intent(mContext, CompleteInfoActivity.class));
                            }
                            dialog.dismiss();
                        }
                    }).create();
            dialog.show();

        }else {
            startActivity(new Intent(mContext, CompleteInfoActivity.class));
        }

    }

    @Override
    public void onGetCodeFailed(String reason) {

    }

    @Override
    public void onCheckCodeSuccess(String token) {

    }

}
